package com.mercury.ec.framework.air.core.client.impl.sunstn.jaxb.adapter;

import cn.hutool.core.util.HexUtil;
import cn.hutool.core.util.StrUtil;

import javax.xml.bind.annotation.adapters.XmlAdapter;
import java.nio.charset.Charset;

/**
 * jaxb 解析16进制字符串适配器
 *
 * @author hesx
 */
public class DecodeHex2XmlAdapter extends XmlAdapter<String, String> {

    @Override
    public String unmarshal(String xml) throws Exception {
        if (StrUtil.isBlank(xml)) {
            return "";
        }
        xml = xml.replaceAll("\\n", "");
        if (xml.length() % 2 != 0) {
            throw new IllegalArgumentException("Invalid hex string length");
        }
        return HexUtil.decodeHexStr(xml, Charset.forName("GB18030"));
    }

    @Override
    public String marshal(String time) throws Exception {
        // todo 暂无需求
        return time;
    }
}
